home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / TransSkel / Demos / C Demos / Button / Document.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  2.7 KB  |  145 lines  |  [TEXT/KAHL]

  1. /*
  2.  * The stuff in this file presents a document window with an outlined
  3.  * pushbutton.  It demonstrates:
  4.  * - Button outlining in a document window.
  5.  *
  6.  * Note that button clicks are tracked and return/enter keyclicks are
  7.  * mapped onto button clicks, but that no other action is associated
  8.  * with those clicks.  This window hander just shows the visible user
  9.  * interface stuff associated with those actions.
  10.  */
  11.  
  12. # include    "TransSkel.h"
  13.  
  14. # include    "Button.h"
  15.  
  16.  
  17. # define    returnKey    13
  18. # define    enterKey    3
  19.  
  20.  
  21. static WindowPtr        wind;
  22. static ControlHandle    okBtn;
  23. static ControlHandle    cancelBtn;
  24.  
  25.  
  26. static pascal void
  27. Mouse (Point pt, long t, short mods)
  28. {
  29. ControlHandle    ctrl;
  30. short    partNo;
  31.  
  32.     if ((partNo = FindControl (pt, wind, &ctrl)) != 0)
  33.     {
  34.         if (partNo == inButton)
  35.         {
  36.             if (TrackControl (ctrl, pt, nil))
  37.             {
  38.                 /* nothing done here */
  39.             }
  40.         }
  41.     }
  42. }
  43.  
  44.  
  45. /*
  46.  * Key handler.  Map return/enter onto clicks in OK button.
  47.  * Note that we check whether the OK button is active or not
  48.  * before flashing the button.  In this application the button
  49.  * is never inactive when the window is active, but that may not
  50.  * be generally true.
  51.  */
  52.  
  53. static pascal void
  54. Key (short c, short code, short mods)
  55. {
  56.     if (c == returnKey || c == enterKey)
  57.     {
  58.         if ((**okBtn).contrlHilite == normalHilite)
  59.         {
  60.             SkelFlashButton (okBtn);
  61.         }
  62.     }
  63. }
  64.  
  65.  
  66. /*
  67.  * Update the window.
  68.  */
  69.  
  70. static pascal void
  71. Update (Boolean resized)
  72. {
  73. WindowPtr    wind;
  74. Rect    r;
  75. short    h;
  76.  
  77.     GetPort (&wind);
  78.  
  79.     r = wind->portRect;
  80.     EraseRect (&r);
  81.     DrawControls (wind);
  82.     SkelDrawButtonOutline (okBtn);
  83. }
  84.  
  85.  
  86. /*
  87.  * Make the buttons active or inactive as the window becomes active or
  88.  * inactive.  Redraw default button outline to follow state of default
  89.  * button.
  90.  */
  91.  
  92. static pascal void
  93. Activate (Boolean active)
  94. {
  95. short    hilite;
  96.  
  97.     hilite = (active ? normalHilite : dimHilite);
  98.     HiliteControl (okBtn, hilite);
  99.     SkelDrawButtonOutline (okBtn);
  100.     HiliteControl (cancelBtn, hilite);
  101. }
  102.  
  103.  
  104. static pascal void
  105. Clobber (void)
  106. {
  107. WindowPtr    wind;
  108.  
  109.     GetPort (&wind);
  110.     HideWindow (wind);
  111.     DisposeWindow (wind);
  112. }
  113.  
  114.  
  115. /*
  116.  * Initialize document window
  117.  */
  118.  
  119. void
  120. SetupDocument (void)
  121. {
  122. Rect    r;
  123.  
  124.     if (SkelQuery (skelQHasColorQD))
  125.         wind = GetNewCWindow (windRes, nil, (WindowPtr) -1L);
  126.     else
  127.         wind = GetNewWindow (windRes, nil, (WindowPtr) -1L);
  128.     SkelWindow (wind,
  129.                 Mouse,
  130.                 Key,
  131.                 Update,
  132.                 Activate,
  133.                 nil,            /* no close box, so no close handler */
  134.                 Clobber,
  135.                 nil,            /* no idle handler */
  136.                 true);            /* irrelevant since no idle handler */
  137.  
  138.     SetRect (&r, 10, 20, 80, 40);
  139.     cancelBtn = NewControl (wind, &r, "\pCancel", true, 0, 0, 1, pushButProc, 0L);
  140.     OffsetRect (&r, 80, 0);
  141.     okBtn = NewControl (wind, &r, "\pOK", true, 0, 0, 1, pushButProc, 0L);
  142.  
  143.     ShowWindow (wind);
  144. }
  145.